/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DinnerOverview.java
 *
 * Created on 21.07.2010, 22:28:18
 */

package info.gamlor.icoodb.desktop.ui.views;

import com.google.inject.Inject;
import info.gamlor.icoodb.desktop.model.Dinner;
import info.gamlor.icoodb.desktop.ui.components.ButtonEditor;
import info.gamlor.icoodb.desktop.ui.components.ButtonRenderer;
import info.gamlor.icoodb.desktop.ui.viewmodel.DinnerListModel;
import info.gamlor.icoodb.desktop.utils.OneArgAction;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 * @author Gamlor
 */
public class DinnerOverview extends DisposablePanel {
    private DinnerListModel model;
    private static final String PACKAGE_PATH = "/info/gamlor/icoodb/desktop/ui/views/";

    public DinnerOverview() {
        initComponents();
    }

    @Inject
    public DinnerOverview(DinnerListModel model) {
        setModel(model);
        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${model.dinners}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dinner}"));
        columnBinding.setColumnName("RSVP");
        columnBinding.setColumnClass(info.gamlor.icoodb.desktop.model.Dinner.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dinner}"));
        columnBinding.setColumnName("Edit");
        columnBinding.setColumnClass(info.gamlor.icoodb.desktop.model.Dinner.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dinner}"));
        columnBinding.setColumnName("Delete");
        columnBinding.setColumnClass(info.gamlor.icoodb.desktop.model.Dinner.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Title");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${count}"));
        columnBinding.setColumnName("RSVPs");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${eventDate}"));
        columnBinding.setColumnName("Date");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${country}"));
        columnBinding.setColumnName("Country");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${address}"));
        columnBinding.setColumnName("Address");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setMinWidth(75);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(75);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(75);
        jTable1.getColumnModel().getColumn(0).setCellEditor(rsvpButtonEditor());
        jTable1.getColumnModel().getColumn(0).setCellRenderer(rsvpButtonRenderer());
        jTable1.getColumnModel().getColumn(1).setMinWidth(75);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(75);
        jTable1.getColumnModel().getColumn(1).setMaxWidth(75);
        jTable1.getColumnModel().getColumn(1).setCellEditor(editButtonEditor());
        jTable1.getColumnModel().getColumn(1).setCellRenderer(editButtonRenderer());
        jTable1.getColumnModel().getColumn(2).setMinWidth(75);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(75);
        jTable1.getColumnModel().getColumn(2).setMaxWidth(75);
        jTable1.getColumnModel().getColumn(2).setCellEditor(deleteButtonEditor());
        jTable1.getColumnModel().getColumn(2).setCellRenderer(deleteButtonRenderer());
        jTable1.getColumnModel().getColumn(3).setMinWidth(75);
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(200);
        jTable1.getColumnModel().getColumn(3).setMaxWidth(500);
        jTable1.getColumnModel().getColumn(4).setMinWidth(25);
        jTable1.getColumnModel().getColumn(4).setPreferredWidth(50);
        jTable1.getColumnModel().getColumn(4).setMaxWidth(125);
        jTable1.getColumnModel().getColumn(5).setMinWidth(50);
        jTable1.getColumnModel().getColumn(5).setPreferredWidth(100);
        jTable1.getColumnModel().getColumn(5).setMaxWidth(125);
        jTable1.getColumnModel().getColumn(6).setMinWidth(50);
        jTable1.getColumnModel().getColumn(6).setPreferredWidth(150);
        jTable1.getColumnModel().getColumn(6).setMaxWidth(200);
        jTable1.getColumnModel().getColumn(7).setMinWidth(75);
        jTable1.getColumnModel().getColumn(7).setPreferredWidth(300);
        jTable1.getColumnModel().getColumn(7).setMaxWidth(500);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                        .addContainerGap())
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public DinnerListModel getModel() {
        return model;
    }

    public void setModel(DinnerListModel model) {
        this.model = model;
    }

    public TableCellRenderer rsvpButtonRenderer() {
        return rsvpButtonFactory();
    }

    public TableCellEditor rsvpButtonEditor() {
        return ButtonEditor.create(rsvpButtonFactory().createButton(), new OneArgAction<Dinner>() {
            @Override
            public void invoke(Dinner arg) {
                getModel().addRSVP(arg);
            }
        });
    }

    private ButtonRenderer rsvpButtonFactory() {
        return createButtonrenderer("RSVP", "add.png");
    }

    public TableCellRenderer editButtonRenderer() {
        return editButtonFactory();
    }

    public TableCellEditor editButtonEditor() {
        return ButtonEditor.create(editButtonFactory().createButton(), new OneArgAction<Dinner>() {
            @Override
            public void invoke(Dinner arg) {
                getModel().edit(arg);
            }
        });
    }

    private ButtonRenderer editButtonFactory() {
        return createButtonrenderer("Edit", "edit.png");
    }

    public TableCellRenderer deleteButtonRenderer() {
        return deleteButtonFactory();
    }

    public TableCellEditor deleteButtonEditor() {
        return ButtonEditor.create(deleteButtonFactory().createButton(), new OneArgAction<Dinner>() {
            @Override
            public void invoke(Dinner arg) {
                getModel().delete(arg);
            }
        });
    }

    private ButtonRenderer deleteButtonFactory() {
        return createButtonrenderer("Delete", "delete.png");
    }

    private ButtonRenderer createButtonrenderer(String title, String imageName) {
        return ButtonRenderer.create(title,
                new ImageIcon(getClass().getResource(PACKAGE_PATH + imageName)));
    }


}
